package org.seckill.web;

import org.seckill.dto.Exposer;
import org.seckill.dto.SeckillExecution;
import org.seckill.dto.SeckillResult;
import org.seckill.entity.Seckill;
import org.seckill.enums.SeckillStatEnum;
import org.seckill.exception.RepeatKillException;
import org.seckill.exception.SeckillCloseException;
import org.seckill.service.SeckillService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/4/2.
 */
@Controller
@RequestMapping("/seckill")
public class SeckillController {
    private final org.slf4j.Logger logger= LoggerFactory.getLogger(this.getClass());


    @Autowired
    private SeckillService seckillService;
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    //获取秒杀列表
    public String list(Model model){
        //获取列表页
        List<Seckill> seckills=seckillService.getSeckillList();
        model.addAttribute("list",seckills);
        return "list";// /WEB-INF/jsp/list.jsp
    }

    //获取详情
    @RequestMapping(value ="/{seckillId}/detail",method = RequestMethod.GET)
    public String detail(@PathVariable("seckillId") long seckillId,Model model){
        if (seckillId==0){
            return "redirect:/seckill/list";//重定向到list
        }
        Seckill seckill=seckillService.getById(seckillId);
        if (seckill==null){
            return "redirect:/seckill/list";//重定向到list
        }
        model.addAttribute("seckill",seckill);


        return "datail";
    }

    //获取是否可以秒杀的信息
    @RequestMapping(value = "/{seckillId}/exposer",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"}
        )

    @ResponseBody//返回json
    public SeckillResult<Exposer> exposer(@PathVariable("seckillId") long seckillId){
            SeckillResult<Exposer> result;
        System.out.println("---------");
            try {
                Exposer exposer=seckillService.exportSeckillUrl(seckillId);
                result=new SeckillResult<Exposer>(true,exposer);

            }catch (Exception e){
                logger.error(e.getMessage(),e);
                result=new SeckillResult<Exposer>(false,e.getMessage());
            }
            return result;
    }

    @RequestMapping(value = "/{seckillId}/{md5}/execution",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"}
    )
    @ResponseBody
    public SeckillResult<SeckillExecution> executeion(@PathVariable("seckillId") long seckillId,@PathVariable("md5") String md5,@CookieValue(value = "userPhone",required = false) Long phone){
        System.out.println(phone);
        if (phone==null){
            return  new SeckillResult<SeckillExecution>(false,"未登录");
        }

        SeckillResult<SeckillExecution> result;
        try{
            SeckillExecution execution=seckillService.executeSeckill(seckillId,phone,md5);
             return new SeckillResult<SeckillExecution>(true,execution);
        }catch (SeckillCloseException e){
            SeckillExecution execution=new SeckillExecution(seckillId,SeckillStatEnum.END);
            return new SeckillResult<SeckillExecution>(false,execution);
        }catch (RepeatKillException e){
            SeckillExecution execution=new SeckillExecution(seckillId,SeckillStatEnum.REPEAT_KILL);
            return new SeckillResult<SeckillExecution>(false,execution);

        }catch (Exception e){
            SeckillExecution execution=new SeckillExecution(seckillId,SeckillStatEnum.INNER_ERROR);
            return new SeckillResult<SeckillExecution>(false,execution);

        }

    }

    @RequestMapping(value = "/time/now",method = RequestMethod.GET)
    @ResponseBody
    public SeckillResult<Long> time(){
        Date now=new Date();
        return new SeckillResult<Long>(true,now.getTime());

    }

}
